package id.co.rho.scoring.cid.server.service.impl;

import id.co.rho.scoring.cid.client.command.impl.TypeModelConfigResult;
import id.co.rho.scoring.cid.domain.TypeModelConfig;
import id.co.rho.scoring.cid.server.dao.GenericDao;
import id.co.rho.scoring.cid.server.dao.TypeModelConfigDao;
import id.co.rho.scoring.cid.server.dao.impl.TypeModelConfigDaoImpl;
import id.co.rho.scoring.cid.server.service.TypeModelConfigService;
import id.co.rho.scoring.cid.shared.model.BaseDTOModel;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.extjs.gxt.ui.client.data.ModelData;

@Service
@Transactional
public class TypeModelConfigServiceImpl implements TypeModelConfigService {
	@PersistenceContext
	EntityManager entityManager;

	private GenericDao genericDao;

	private TypeModelConfigDao typeModelConfigDAO;

	@Autowired
	public void setGenericDao(GenericDao genericDao) {
		this.genericDao = genericDao;
	}

	@Autowired
	public void setTypeModelConfigDAO(TypeModelConfigDao typeModelConfigDAO) {
		this.typeModelConfigDAO = typeModelConfigDAO;
	}

	@Override
	public TypeModelConfig saveOrUpdate(TypeModelConfig model) {
		TypeModelConfig result = null;
		result = genericDao.saveOrUpdate(model);
		return result;
	}

	public TypeModelConfigResult searchByCodeAndName(String code, String name) {
		if (code == null)
			code = "";
		if (name == null)
			name = "";
		TypeModelConfigResult result = new TypeModelConfigResult();
		final List<TypeModelConfig> typeModelConfigs = typeModelConfigDAO
				.searchByCodeAndName(code, name);
		List<ModelData> models = new ArrayList<ModelData>();
		for (int i = 0; i < typeModelConfigs.size(); i++) {
			TypeModelConfig model = typeModelConfigs.get(i);
			ModelData md = new BaseDTOModel();
			md.set("id", model.getId());
			md.set("name", model.getName());
			md.set("code", model.getCode());
			md.set("descr", model.getDescr());
			models.add(md);
		}
		result.setModels((Serializable) models);
		return result;
	}

	@Override
	public TypeModelConfig getTypeModelConfigById(Long id) {
		TypeModelConfig model = genericDao.find(TypeModelConfig.class, id);
		return model;
	}

	@Override
	public TypeModelConfigResult findAll() {
		TypeModelConfigResult result = new TypeModelConfigResult();
		final List<TypeModelConfig> typeModelConfigs = typeModelConfigDAO.findAll();
		List<ModelData> models = new ArrayList<ModelData>();
		for (int i = 0; i < typeModelConfigs.size(); i++) {
			TypeModelConfig model = typeModelConfigs.get(i);
			ModelData md = new BaseDTOModel();
			md.set("id", model.getId());
			md.set("name", model.getName());
			md.set("code", model.getCode());
			md.set("descr", model.getDescr());
			models.add(md);
		}
		result.setModels((Serializable) models);
		return result;
	}
}
